<?php

namespace app\Api\controller;

use app\common\controller\Api;

class RoomClass extends Api
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {        
        $room_class_model = model('room_class');
        $this->result['data'] = $room_class_model->all();
        return $this->result;
    }
    
    public function getClassRoom()
    {
        $this->result['data'] = model('room')->where('class_id', '=', $this->request->post('class_id'))->field(['intro','taocan'],true)->order('sort asc')->select();
        $roomBookingModel = new \app\common\model\RoomBooking();
        $hour=date("G");
        if($hour < 3){
            $begintime=strtotime(date("Y-m-d",strtotime("-1 day"))." 0:0:0");
            $endtime=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");
        }else{
            $begintime=strtotime(date("Y-m-d",time())." 0:0:0");
            $endtime=strtotime(date("Y-m-d",time())." 23:59:59");
        }
        $where = [];
        $where[] = ['booking_time', ['EGT',$begintime],['ELT',$endtime],'and'];
        $where[] = [ 'room_id','in', array_column(obj_to_array($this->result['data']), 'id')];
        $where[] = ['pay_status', '=', 1];
        $booking_list = $roomBookingModel->field('*')->where($where)->select();
        $booking_list = arr_key($booking_list, 'room_id');
         //遍历获取房间状态
        foreach ($this->result['data'] as &$info) {
            $booking = $booking_list[$info['id']];
            if(!empty($booking)) {
                switch ($booking['to_shop']) {
                    case '1':
                        $info['type'] = 3;
                        break;
                    case '2':
                        $info['type'] = 1;
                        break;
                    default :
                        $info['type'] = 2;
                }
            } else {
                $info['type'] = 1;
            }
        }
        return $this->result;
    }
}
